<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Admin\CommonController;
use App\Http\Controllers\Admin\JwtAuth;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class LoginToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->header('token');
//        if (session()->has('token')==false||session('token')!=$request->header('token')){
//            return response()->json(['code'=>-2,'msg'=>'error','data'=>'登录验证过期']);
//        }
        if ($token){
            $jwtAuth= JwtAuth::getInstance();
            $jwtAuth->setToken($token);
            if ($jwtAuth->validate() && $jwtAuth->verify()){
                $res_url = explode('?', $request->getRequestUri());
                $permission_list = $this->GetPermissionList($token);
                if (!in_array($res_url[0],$permission_list)){
                    //不在路由组 视为非法请求
                    return response()->json(['code'=>-3,'msg'=>'error','data'=>'非法访问']);
                }
                return $next($request);
            }else{
                //token验证失败
                return response()->json(['code'=>-2,'msg'=>'error','data'=>'登陆过期']);
            }


        }else{
            return response()->json(['code'=>-2,'msg'=>'error','data'=>'参数不完整']);
        }

    }

    //获取当前用户权限
    public function GetPermissionList($token){
        $common = new CommonController();
        $res=DB::table('user as a')
            ->leftJoin('user_role as b','a.id','=','b.user_id')
            ->leftJoin('role as c','c.id','=','b.role_id')
            ->leftJoin('permission_role as d','d.role_id','=','c.id')
            ->leftJoin('permission as e','e.id','=','d.permission_id')
            ->where('a.delete_flag','=',0)
            ->where('a.status','=',0)
            ->where('b.delete_flag','=',0)
            ->where('b.status','=',0)
            ->where('c.delete_flag','=',0)
            ->where('c.status','=',0)
            ->where('d.delete_flag','=',0)
            ->where('d.status','=',0)
            ->where('e.delete_flag','=',0)
            ->where('e.status','=',0)
            ->where('a.id','=',$common->getUserId($token))
            ->select('e.route')
            ->get()
            ->toArray();
        $list=[];
        if (!empty($res)){
            foreach ($res as $key=>$val){
                //过滤空路由
                if (strlen($val->route)>0){
                    $arr_route=explode(',',$val->route);
                    foreach ($arr_route as $k=>$v){
                        $list[]=$v;
                    }

                }
            }
        }
        //去重
        $result = array_unique($list);
//        Log::error($result);
        return $result;

    }

//    /**
//     * @param $token
//     * @return mixed
//     * 拿到用户id
//     */
//    public function getUserId($token){
//        $jwtAuth = JwtAuth::getInstance();
//        return $jwtAuth->setToken($token)->getUid();
//
//    }
}
